@using Microsoft.AspNetCore.Http.Features
@using Lombiq.Privacy.Controllers

@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery AntiForgery

<style asp-name="@ResourceNames.ConsentBanner"></style>
<script asp-name="jQuery"></script>

@{
    const string privacyConsent = "privacy-consent";
    const string bannerId = privacyConsent + "-banner";
    const string acceptButtonId = privacyConsent + "-accept-button";
    const string deniedButtonId = privacyConsent + "-denied-button";

    var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
    var acceptCookie = consentFeature?.CreateConsentCookie();
    var deniedCookie = acceptCookie.Replace("yes", "no");
    var requestToken = AntiForgery.GetAndStoreTokens(Context).RequestToken;
}

<shape Type="Lombiq_Privacy_ConsentBanner_Resources" />

<div id="@bannerId" class="privacy-consent-banner">
    <div class="modal-content modal">
        <div class="modal-header bg-secondary">
            <h5 class="modal-title text-white">@T["Privacy consent"]</h5>
        </div>
        <div class="modal-body">
            <shape Type="Lombiq_Privacy_ConsentBanner_Text" />
        </div>
        <div class="modal-footer justify-content-center">
            <div class="btn-group btn-group-lg" role="group">
                <button id="@acceptButtonId" type="button" class="btn btn-success" data-cookie-string="@acceptCookie">
                    @T["I agree, do not show me this anymore"]
                </button>
                @if (!User.Identity.IsAuthenticated)
                {
                    <button id="@deniedButtonId" type="button" class="btn btn-secondary" data-cookie-string="@deniedCookie">
                        @T["I don't agree, do not show me this anymore"]
                    </button>
                }
            </div>
        </div>
    </div>
</div>

@if (!User.Identity.IsAuthenticated)
{
    <script depends-on="jQuery" at="Foot">
        jQuery(($) => {
            $('#@acceptButtonId').click(function () {
                document.cookie = $('#@acceptButtonId').data('cookie-string');
                $('#@bannerId').remove();
            });

            $('#@deniedButtonId').click(function () {
                document.cookie = $('#@deniedButtonId').data('cookie-string');
                $('#@bannerId').remove();
            });
        });
    </script>
}
else
{
    var acceptanceOfConsentURL = Url.Action(
        nameof(PrivacyConsentController.AcceptanceOfConsent),
        typeof(PrivacyConsentController).ControllerName(),
        new { area = $"{nameof(Lombiq)}.{nameof(Lombiq.Privacy)}" });

    <script depends-on="jQuery" at="Foot">
        jQuery(($) => {
            $('#@acceptButtonId').click(function () {
                $.post(
                    @acceptanceOfConsentURL.JsonHtmlContent(),
                    @Json.Serialize(new { __RequestVerificationToken = requestToken }))
                    .done(function () { $('#@bannerId').remove(); });
            });
        });
    </script>
}
